<?php

declare(strict_types=1);

namespace app\command;

use app\service\product\ProductImportJobService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

class ProductImport extends Command
{
    protected function configure()
    {
        $this->setName('product:import')
            ->setDescription('处理待导入的商品任务')
            ->addOption('limit', 'l', Option::VALUE_OPTIONAL, '本次处理的任务数量', 1);
    }

    protected function execute(Input $input, Output $output)
    {
        $limit = (int) $input->getOption('limit') ?: 1;
        /** @var ProductImportJobService $service */
        $service = app(ProductImportJobService::class);

        $stats = $service->processPendingJobs($limit);

        $output->writeln(sprintf(
            '任务总数: %d, 已处理: %d, 失败: %d',
            $stats['total'],
            $stats['processed'],
            $stats['failed']
        ));

        return 0;
    }
}
